package com.molyfun.parents.fragment.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.SignUpNoticeResponse;
import com.molyfun.parents.view.StrokeTextView;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;


public class OrganizationSignUpListFragment extends DialogFragment implements View.OnClickListener {

    private static final int MAX_SIGN_NUM = 3;

    @InjectView(R.id.content)
    LinearLayout mInfoContainerLayout;
    @InjectView(R.id.submit)
    StrokeTextView mSubmit;
    @InjectView(R.id.submit_continue)
    StrokeTextView mSubmitContinue;


    private static OrganizationSignUpListFragment newInstance(ArrayList<SignUpNoticeResponse.SignUpInfo> infoList) {
        OrganizationSignUpListFragment mStudentMenuDialogFragment = new OrganizationSignUpListFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("infoList", infoList);
        mStudentMenuDialogFragment.setArguments(bundle);
        return mStudentMenuDialogFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (null != dialog) {
            dialog.getWindow().setLayout(-1, -2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        window.setAttributes(lp);

        View view = inflater.inflate(R.layout.dialog_organization_sign_up_list, container, false);
        ButterKnife.inject(this, view);
        initView();
        return view;
    }

    private void initView() {
        mSubmit.setOnClickListener(this);
        mSubmitContinue.setOnClickListener(this);

        ArrayList<SignUpNoticeResponse.SignUpInfo> signUpInfos = getArguments().getParcelableArrayList("infoList");
        if(signUpInfos.size() >= 3){
            mSubmitContinue.setVisibility(View.GONE);
        }else{
            mSubmitContinue.setVisibility(View.VISIBLE);
        }

        if(signUpInfos.size() > 0){
            for(SignUpNoticeResponse.SignUpInfo signUpInfo : signUpInfos){
                View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_signup_info_cell, null, false);
                ((TextView)view.findViewById(R.id.student_name)).setText(signUpInfo.getName());
                if(signUpInfo.isSortflage()){
                    ((TextView)view.findViewById(R.id.sort_num)).setText("排名" + signUpInfo.getNumber());
                }else{
                    ((TextView)view.findViewById(R.id.sort_num)).setVisibility(View.GONE);
                }

                TextView signUpState = (TextView) view.findViewById(R.id.signup_state);
                signUpState.setText(signUpInfo.getStatus());

                mInfoContainerLayout.addView(view);
            }
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit:
                if(mOrganizationSignUpCallback != null){
                    mOrganizationSignUpCallback.onCancel();
                }
                getDialog().dismiss();
                break;
            case R.id.submit_continue:
                if(mOrganizationSignUpCallback != null){
                    mOrganizationSignUpCallback.onSignUpContinue();
                }
                getDialog().dismiss();
                break;
            default:
                break;
        }

    }

    private OrganizationSignUpCallback mOrganizationSignUpCallback;

    public interface OrganizationSignUpCallback{
        void onSignUpContinue();
        void onCancel();
    }

    public void setOrganizationSignUpCallback(OrganizationSignUpCallback organizationSignUpCallback){
        this.mOrganizationSignUpCallback = organizationSignUpCallback;
    }

    public static class OrganizationSignUpListDialogBuilder {

        OrganizationSignUpListFragment mFromsDialogFragment;
        private String mTag;

        public OrganizationSignUpListDialogBuilder(ArrayList<SignUpNoticeResponse.SignUpInfo> signUpInfos) {
            mFromsDialogFragment = OrganizationSignUpListFragment.newInstance(signUpInfos);
            mTag = "organization_signup_info";
        }

        public OrganizationSignUpListDialogBuilder setTag(String tag) {
            this.mTag = tag;
            return this;
        }

        public OrganizationSignUpListDialogBuilder setOrganizationSignUpCallback(OrganizationSignUpCallback organizationSignUpCallback) {
            mFromsDialogFragment.setOrganizationSignUpCallback(organizationSignUpCallback);
            return this;
        }

        public void build(FragmentTransaction fragmentTransaction) {
//            mCommonDialogFragment.show(fragmentTransaction, mTag);
            fragmentTransaction.add(mFromsDialogFragment, mTag);
            fragmentTransaction.commitAllowingStateLoss();
        }
    }

}
